package com.haoran.grope;

import javax.annotation.PostConstruct;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StopWatch;

import com.haoran.grope.client.RabbitMQClient;

@SpringBootApplication
public class RabbitMQTopicApplication {

	public static void main(String[] args) {
		SpringApplication.run(RabbitMQTopicApplication.class, args);
	}

	@Autowired
	private RabbitMQClient client;

	@PostConstruct
	public void init() {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		for (int i = 0; i < 10; i++) {
			if (i % 4 == 0) {
				client.send("send USM error message:" + i, "usm.error");
			} else if (i % 4 == 1) {
				client.send("send USM info message:" + i, "usm.info");
			} else if (i % 4 == 2) {
				client.send("send USM debug message:" + i, "usm.debug");
			} else if (i % 4 == 3) {
				client.send("send API error message:" + i, "api.error");
			} else {
				client.send("send API info message" + i, "api.info");
			}
		}
		stopWatch.stop();
		System.out.println("总共耗时：" + stopWatch.getTotalTimeMillis());
	}

	@Bean("usm")
	public Queue queue1() {
		return new Queue("usm");
	}

	@Bean("api")
	public Queue queue2() {
		return new Queue("api");
	}

	@Bean("error")
	public Queue queue3() {
		return new Queue("error");
	}

	@Bean
	public TopicExchange exchange() {
		return new TopicExchange("amq-topic-exchange");
	}

	@Bean
	public Binding binding(@Qualifier("usm") Queue queue, TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("usm.*");
	}

	@Bean("b2")
	public Binding binding1(@Qualifier("api") Queue queue, TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("api.*");
	}
	
	@Bean
	public Binding binding2(@Qualifier("error") Queue queue, TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("*.error");
	}
}
